package com.ojoin.trade.t0loan_core.web.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import org.springframework.data.domain.Pageable;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanApplication;
import com.ojoin.trade.t0loan_core.web.dao.TbFtpFileDao;
import com.ojoin.trade.t0loan_core.web.domain.TbFtpFile;
import com.ojoin.trade.t0loan_core.web.domain.TbPosTranscationLog;

@Repository
public class TbFtpFileDaoImpl extends BaseDaoImpl<TbFtpFile> implements TbFtpFileDao {

	public SqlSession getSqlSession() {
		return this.sqlSessionTemplate;
	}

	@Override
	public int updateByVO(TbFtpFile vo) throws BusinessException {
  
		 String mapMethodName = "updateByVO";
	        try{
	            return this.sqlSessionTemplate.update(getSqlName(mapMethodName),vo);
	        }catch (Exception e) {
	            throw new DaoException(String.format("更新FTP文件下载处理记录出错！语句：%s",
	                    getSqlName(mapMethodName)), e);
	        }
	        
	}
	
	@Override
	public int updateDealFlagByID(int id, int dealflag, String dealtime)
			throws BusinessException {
		String mapMethodName = "updateDealFlagByID";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("id", id);
			paramMap.put("dealflag", dealflag);
			paramMap.put("dealtime", dealtime);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新FTP文件处理状态出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public TbFtpFile selectDealedFtpFileByName(String filename) throws BusinessException {
		String mapMethodName = "selectDealedFtpFileByName";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("filename", filename);
			
			List<TbFtpFile> list = getSqlSession().selectList(getSqlName(mapMethodName), paramMap);
			if (list != null && list.size() > 0)
				return list.get(0);
		} catch (Exception e) {
			throw new DaoException(String.format("文件名" + filename + " 查询处理过的FTP文件出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		return null;
	}

	@Override
	public TbFtpFile selectFtpFileByJoinuseridAndTransdate(
			int joinuserid, String transdate) throws BusinessException {

		String mapMethodName = "selectFtpFileByJoinuseridAndTransdate";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("joinuserid", joinuserid);
			paramMap.put("transdate", transdate);

			return sqlSessionTemplate.selectOne(
						getSqlName(mapMethodName),
						paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	
	}

	@Override
	public List<TbFtpFile> selectFtpFileListByCondition(String filetype, Integer downloadflag, Integer dealflag, Integer joinuserid, String startdate, String enddate, Pageable page) throws BusinessException {
		String mapMethodName = "selectFtpFileListByCondition";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("filetype", filetype);
			paramMap.put("downloadflag", downloadflag);
			paramMap.put("dealflag", dealflag);
			paramMap.put("idOfTbMerchantUser", joinuserid);
			paramMap.put("startdate", startdate);
			paramMap.put("enddate", enddate);
			
			return getSqlSession().selectList(getSqlName(mapMethodName), paramMap, this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("按条件查询FTP文件出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
}
